#include"BinaryTree.h"


BTNode* CreatBinaryTree()
{
    BTNode* node1 = BuyNode(1);
    BTNode* node2 = BuyNode(2);
    BTNode* node3 = BuyNode(3);
    BTNode* node4 = BuyNode(4);
    BTNode* node5 = BuyNode(5);
    BTNode* node6 = BuyNode(6);

    node1->lift = node2;
    node1->right = node4;
    node2->lift = node3;
    node4->lift = node5;
    node4->right = node6;

    return node1;
}
//求所有节点的个数 用全局方式求
int count = 0;
void TreeSize(BTNode* root)
{
    if(root == NULL)
    {
        return ;
    }
    TreeSize(root->lift);
    count++;
    TreeSize(root->right);

}
int TreeSize1(BTNode* root)
{
    if(root == NULL)
    {
        return 0;
    }

    return TreeSize1(root->lift) + TreeSize1(root->right) + 1;
}
int TreeLeafSize(BTNode* root)
{
    if(root == NULL)
    {
        return 0;
    }
    return (root->lift== NULL && root->right == NULL) ? 1 : TreeLeafSize(root->lift) + TreeLeafSize(root->right);
}
int TreeKLevel(BTNode* root, int k)
{
    if(root == NULL)
    {
        return 0;
    }
    if(k == 1)
    {
        return 1;
    }
    return TreeKLevel(root->lift, k - 1) + TreeKLevel(root->right, k - 1);
}

BTNode* TreeFind(BTNode* root, BTDatatype k)
{
    if(root == NULL)
    {
        return NULL;
    }

    if(root->data == k)
    {
        return root;
    }

    BTNode* ret1 = TreeFind(root->lift, k);
    if(ret1) return ret1;
    BTNode* ret2 = TreeFind(root->right, k);
    if(ret2) return ret2;

    return NULL;

}
void BinorytreeTest1()
{
    BTNode* root = CreatBinaryTree();
    preorder(root);
    printf("\n");
    inorder(root);
    printf("\n");
    postorder(root);
    printf("\n");
    TreeSize(root);
    printf("%d \n", count);
    TreeSize(root);
    printf("%d \n", count);
    printf("%d\n", TreeSize1(root));
    printf("%d\n",TreeLeafSize(root));
    printf("%d\n",TreeKLevel(root, 2));
    BTNode* node = TreeFind(root, 5); 
    printf("%d \n", node->data);
}


int main()
{
     BinorytreeTest1();
    return 0;
}
